package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import dao.AddItemDAO;
import dao.GetItemDAO;
import dao.ReviseItemDAO;
import databean.ItemBean;
import formbean.ReviseItemForm;

public class ReviseItemAction extends Action {

	@Override
	public String perform(HttpServletRequest request,
			HttpServletResponse response) {

	
		String id=request.getParameter("id");
		// case the first time come
		if (!ServletFileUpload.isMultipartContent(request)) {
			ItemBean item=GetItemDAO.getItem(id);
			request.setAttribute("id", id);
			request.setAttribute("title", item.getTitle());
			request.setAttribute("description", item.getDescription());
			request.setAttribute("price", item.getStartPrice());
			request.setAttribute("buyItNowPrice", item.getBuyItNowPrice());
			return "reviseItem.view";
		}

		// get user input information
		ItemBean item = ReviseItemForm.parse(request);

		// update to ebay
		if (item != null) {
			
			String rs = ReviseItemDAO.reviseItem(item);

			if (rs == null) {
				request.setAttribute("error", ReviseItemDAO.getMessage());
				return "reviseItem.view";
			} else {
				request.setAttribute("rs", rs);
			}
		}
		return "ListItem.do";
	}

}
